import requests
from lxml import etree
import csv

# 隧道域名:端口号
tunnel = "XXX.XXX.com:15818"

# 用户名密码方式
info = 't14409688113668:hyn69tje@j547.kdltps.com:15818'
proxies = {
	"http": f"http://{info}",
	"https": f"http://{info}"
}

# res = requests.get("http://httpbin.org/get", proxies=proxies)
# print(f"IP: {res.json()['origin']}")


start = 0
headers = {
	"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36"
}
datas = []

while True:
	try:
		print(f"当前偏移{start}.......")
		res = requests.get(f"https://movie.douban.com/top250?start={start}&filter=", headers=headers, proxies=proxies)
		tree = etree.HTML(res.text)

		if "004" in res.text:
			raise Exception(f"004 异常")
		elif "0003" in res.text:
			raise Exception("f0003 异常")
		else:
			title = tree.xpath('//title/text()')
			if title:
				if "登录跳转页" in title[0]:
					raise Exception(f"跳转异常")
				else:
					items = tree.xpath('//ol[@class="grid_view"]/li/div[@class="item"]')
					for item in items:
						title = item.xpath('.//div[@class="hd"]//span[@class="title"]/text()')[0]
						datas.append([1 if not datas else datas[-1][0] + 1, title])
					start += 25
					if len(datas) == 250:
						break

			else:
				print(res.text)
				raise Exception("未识别到title的其他异常.....")
	except Exception as e:
		print(f"异常 {e}")


print(datas)

